package com.sun.controller.ums;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.common.MsResult;
import com.sun.pojo.UmsMenu;
import com.sun.service.ums.IUmsMenuService;
import com.sun.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author 妙妙屋
 * @since 2021-12-13
 */
@RestController
@RequestMapping("/menu")
public class UmsMenuController {
    @Autowired
    private IUmsMenuService umsMenuService;

    @GetMapping("/list/{pageId}")
    // @PathVariable 得到路径后面的参数
    public MsResult list(@PathVariable Long pageId,Long pageNum,Long pageSize ){
        IPage<UmsMenu> page = umsMenuService.selectPage(pageId,pageNum,pageSize);
        PageVo vo = PageVo.pageToVo(page);
        return MsResult.isOkData(vo);
    }

    @PostMapping("/create")
    public MsResult crate(@RequestBody UmsMenu umsMenu){
        umsMenu.setCreateTime(LocalDateTime.now());

        //知道当前菜单的父级id（parentId），查询到父级的级别
        Integer level = null;
        if(umsMenu.getParentId()==0){
            level = 0;
        }else {
            UmsMenu byId = umsMenuService.getById(umsMenu.getParentId());
            level = byId.getLevel() + 1;
        }
        umsMenu.setLevel(level);
        boolean ok = umsMenuService.save(umsMenu);
        if(ok){
            return MsResult.isOkMessage("添加菜单成功");
        }
        return MsResult.isFail("菜单添加失败");
    }

    @GetMapping("/treeList")
    public MsResult treeList (){
        List<UmsMenu> menuList = umsMenuService.getTreeList();
        return MsResult.isOkData(menuList);
    }
}

